package ManipullateDatabases;

import ConnectionsDatebases.Dbutils;
import Factory.FactoryBuilder;
import Car.Car.Car;
import Introspector.Introspector;
import jdk.nashorn.internal.ir.CatchNode;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OperatorCar {
    private static Map<String,String> carToType = null;
    public OperatorCar(){
        carToType = new HashMap<>();
        carToType.put("货车","Hc");
        carToType.put("客车","Kc");
        carToType.put("皮卡车","Pkc");
    }
    //往cars表中加入数据
    public static void insertCar(String carName, String typeName, String passenger_size, String carry_size, String piece) {
        Connection con = Dbutils.Connect();
        int typeId = OperatorCType.getTypeId(typeName);
        String sql = "insert into cars values (null,'" + carName + "'," + typeId + "," + passenger_size + "," + carry_size + "," + piece + ")";
        PreparedStatement pstmt = Dbutils.getPrepareStatement(con, sql);
        try {
            pstmt.executeUpdate();
        } catch (SQLException | NullPointerException e) {
            e.printStackTrace();
        }
        Dbutils.closePreparestatement(pstmt);
        Dbutils.closeConnection(con);
        addCarOfCartType(typeId);
    }

    //没获得一辆车就对应的在对应车的类型上数量加一
    private static void addCarOfCartType(int typeId) {

        Connection con = Dbutils.Connect();
        String sql = "update cartype set carNum=carNum+1 where typeId =" + typeId;
        PreparedStatement pstmt = Dbutils.getPrepareStatement(con, sql);
        try {
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Dbutils.closePreparestatement(pstmt);
        Dbutils.closeConnection(con);
    }

    //根据类型的id获取给类型的所有车
   public static List<Car> selectCars(String typeName1) {
//        System.out.println(typeName1);
        carToType = new HashMap<>();
        carToType.put("货车","Hc");
        carToType.put("客车","Kc");
        carToType.put("皮卡车","Pkc");
        Connection con = Dbutils.Connect();
        String  sql = "select * from cars inner JOIN cartype WHERE cars.typeId=cartype.typeId and cartype.typeName ='"+typeName1+"'";
        PreparedStatement pstmt = Dbutils.getPrepareStatement(con, sql);
        ResultSet rs = null;
        List<Car> cars = new ArrayList<Car>();
        try {
            rs = pstmt.executeQuery();
            while (rs.next()) {
                String typeName =carToType.get(typeName1);
//                System.out.println(typeName);
                Car car = FactoryBuilder.create(typeName);
                String name =rs.getString(2);
                int Passenger_size =rs.getInt(4);
                double carry_size = rs.getDouble(5);
                double price = rs.getDouble(6);
                Introspector.setProperty(car,"name",name);
                Introspector.setProperty(car,"typeName",typeName);
                Introspector.setProperty(car,"Passenger_size",Passenger_size);
                Introspector.setProperty(car,"carry_size",carry_size);
                Introspector.setProperty(car,"price",price);
                System.out.println(car);
                cars.add(car);
            }
        } catch (SQLException | NullPointerException e) {
            e.printStackTrace();
        }
        return cars;
    }


    //清空cars表中的数据
    public static void deleteCars() {
        Connection con = Dbutils.Connect();
        String sql = "delete from cars";
        PreparedStatement pstmt = Dbutils.getPrepareStatement(con, sql);
        try {
            pstmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("没有数据可以删除");
        }
    }

    public static boolean InquireOfCarType(int typeId, int count) {
        boolean flag = false;
        Connection con = Dbutils.Connect();
        String sql = "select carNum from cartype where typeId =" + typeId + ";";
        PreparedStatement pstmt = Dbutils.getPrepareStatement(con, sql);
        ResultSet rs = null;
        int num = 0;
        try {
            rs = pstmt.executeQuery();
            while (rs.next()) {
                num = rs.getInt(1);
            }
            if (num > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        num -= count;
        if (num < 0) {
            flag = false;
        }
        if (flag) {
            updateCarNumOf(typeId, num);
        }
        Dbutils.closePreparestatement(pstmt);
        Dbutils.closeConnection(con);
        return flag;
    }

    public static void updateCarNumOf(int typeId, int num) {
        Connection con = Dbutils.Connect();
        String sql = "update cartype set carNum =" + num + " where typeId =" + typeId;
        PreparedStatement pstmt = Dbutils.getPrepareStatement(con, sql);
        try {
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Dbutils.closePreparestatement(pstmt);
        Dbutils.closeConnection(con);
    }
}
